#pragma once
#include "Comm.hpp"

class Init
{
public:
    Init()
    {
        umask(0);
        int n = ::mkfifo(gpipefile.c_str(), gmode);
        if (n < 0)
        {
            std::cerr << "mkfifo error" << std::endl;
            return;
        }
        std::cout << "mkfifo sucess" << std::endl;
    }

    ~Init()
    {
        int n = ::unlink(gpipefile.c_str());
        if (n < 0)
        {
            std::cerr << "unlink error" << std::endl;
            return;
        }
        std::cout << "unlink sucess" << std::endl;
    }
};

Init init;

class Sever
{
public:
    Sever(int fd = gdefaultfd)
        : _fd(fd)
    {
    }
    
    bool OpenForRead()
    {
        _fd = OpenPipeFile(gForreadopen);
        if(_fd < 0) return false;
        return true;
    }
    //输出型参数
    int Recpipe(std::string *out)
    {
        char buf[gsize];
        ssize_t n = ::read(_fd,buf,sizeof(buf)-1);
        if(n > 0)
        {
            buf[n] = 0;
            *out = buf;
        }
        return n;
    }

    void ClosePipe()
    {
        ClosePipeHelper(_fd);
    }
    ~Sever()
    {}
private:
    int _fd;
};